<template>
  <div class="role">
    <!-- 按钮 -->
    <div class="btns">
      <el-button size="small" type="primary" @click="toAdd">添加</el-button>
      <el-button size="small" type="warning" @click="loadDate">刷新</el-button>
    </div>
    <!-- 表格 -->
  
    <el-table :data="list" style="width: 100%" size="small">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="name" label="名称" width="70"> </el-table-column>
      <el-table-column label="操作 " width="150" align="center"> 
        <template v-slot="scope">
          <el-button size="small" type="text" @click="del(scope.row)">删除</el-button>
          <el-button size="small" type="text" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 模态框 -->
    <el-dialog
    class="customer_modal"
      title="添加文章"
      :visible.sync="dialogVisible"
      width="60%"
      >
      {{form}}
        <el-form :model="form" label-width="100px">
      <el-form-item label="名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
     
      
      
    </el-form>


      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false" size="small">确 定</el-button>
        <el-button type="primary" @click="save" size="small">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

//导入request，浏览器提供与后台进行交互的工具
import {get} from '@/utils/request'
export default {
  data (){
    return {
      form:{},
      list:[],
      dialogVisible:false
    }
  },
  created(){
    this.loadDate();
  },
  methods:{
//保存
save(){
        let url = "/cms_role/saveOrUpdate"
      get(url,this.form).then( (response)=>{
        // 1. 模态框关闭
        this.dialogVisible = false;
        // 2. 提示
        this.$message({type:"success",message:response.message})
        // 3. 刷新
        this.loadData();
      } )

},
    
      






//删除
    del(row){
      let url ="/cms_role/deleteById"
      get(url,{id:row.id}).then((response)=>{
        //提示
        this.$message({type:"success",message:response.message})
      })
       //刷新
       this.loadDate();
    },
//编辑
    edit(row){
            this.form = row;
      this.dialogVisible = true;
    },
    loadDate(){
      let url ="/cms_role/findAll";
      get(url).then((response)=>{
        this.list=response.data;
      });
    },
    toAdd(){
      this.dialogVisible = true;
    }
  }
}
</script>